<?
/**
 * rsstyles.php - Compressor/Concatenator for optimizing CSS load time
 *
 * Combines concatenation, CSS minimizing, buffered gzip output and both
 * server-side and client-side caching, all of which are optional.
 *
 * PHP 5 or higher is required.
 *
 * Permission is hereby granted to use this script under the conditions of the
 * MIT License.
 *
 * --
 * Copyright (c) 2009 David Beveridge (http://www.nerdyisback.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * The Software shall be used for Good, not Evil.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * --
 *
 * @package ResourceSquisher
 * @author David Beveridge <davidjbeveridge@gmail.com>
 * @copyright 2009 David Beveridge <davidjbeveridge@gmail.com>
 * @license http://opensource.org/licenses/mit-license.php MIT License
 * @version 0.2 (2008-03-02)
 */

require_once(dirname(__FILE__).'/RS_config.php');
require_once(dirname(__FILE__).'/jsmin.php');

if(RS_USE_GZIP_COMPRESSION)	{
	ob_start('ob_gzhandler');
}
header("Content-type: text/javascript; charset: UTF-8");
header("Cache-Control: must-revalidate");
header("Expires: " .gmdate("D, d M Y H:i:s",time() + RS_CLIENT_CACHE_DURATION) . " GMT");

if(isset($_REQUEST[RS_SCRIPT_ARG_NAME]) && !empty($_REQUEST[RS_SCRIPT_ARG_NAME]))	{
	$cacheFilename = RS_SERVER_CACHE_PATH.md5('js'.$_REQUEST['files']).'.tmp';
	if(RS_USE_SERVER_CACHING && file_exists($cacheFilename) && (time() > (filectime($file)+RS_CLIENT_CACHE_DURATION)))	{
		readfile($cacheFilename);
	}
	else	{
		$files = explode(',', $_REQUEST[RS_SCRIPT_ARG_NAME]);
		$output = '';
		foreach($files as $file)	{
			$filepath = RS_CSS_PATH."$file.js";
			if( file_exists( $filepath ) )	{
				$currentContents = file_get_contents($filepath);
				if(RS_USE_MINIMIZING)	{
					$output .= jsmin::minify($currentContents);
				}
				else	{
					$output .= $currentContents;
				}
			}
		}
		if(RS_USE_SERVER_CACHING)	{
			file_put_contents($cacheFilename, $output);
		}
		echo $output;
	}
}
